﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Macaw.Wss3.DependencySpiderSolution;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint.Utilities;

namespace Macaw.Wss3.DependencySpiderSolution
{
    public partial class DependencySpiderConfiguration : LayoutsPageBase
    {
        protected AssetUrlSelector selReportingList;
        protected CheckBox cbEnabled;
        protected CheckBox cbSpiderEnabled;
        protected CheckBox cbStoreMap;
        protected TextBox tbExcludeCrawlRules;
        protected TextBox tbIncludeCrawlRules;
        protected Label lblStatus;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void SyncControls()
        {
            using (SPWeb web = Site.OpenWeb(Site.RootWeb.ID))
            {
                #region Checkbox Enabled
                if (cbEnabled != null)
                {
                    if (!web.Properties.ContainsKey(DependencySpiderFeatureReceiver.DEPENDENCYSPIDERENABLEDKEY))
                    {
                        cbEnabled.Checked = false;
                    }
                    else
                    {
                        bool enabledstate = false;
                        if (bool.TryParse(web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERENABLEDKEY], out enabledstate))
                        {
                            cbEnabled.Checked = enabledstate;
                        }
                        else
                        {
                            cbEnabled.Checked = false;
                        }
                    }
                }
                #endregion

                #region ReportingList selector
                if (selReportingList != null)
                {
                    if (!web.Properties.ContainsKey(DependencySpiderFeatureReceiver.DEPENDENCYSPIDERREPORTKEY))
                    {
                        selReportingList.AssetUrl = null;
                    }
                    else
                    {
                        selReportingList.AssetUrl = web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERREPORTKEY];
                    }
                }
                #endregion

                #region Checkbox StoreMap
                if (cbStoreMap != null)
                {
                    if (!web.Properties.ContainsKey(DependencySpiderFeatureReceiver.DEPENDENCYSPIDERSTOREMAPKEY))
                    {
                        cbStoreMap.Checked = false;
                    }
                    else
                    {
                        bool enabledstate = false;
                        if (bool.TryParse(web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERSTOREMAPKEY], out enabledstate))
                        {
                            cbStoreMap.Checked = enabledstate;
                        }
                        else
                        {
                            cbStoreMap.Checked = false;
                        }
                    }
                }
                #endregion

                #region Checkbox Spider Enabled
                if (cbSpiderEnabled != null)
                {
                    if (!web.Properties.ContainsKey(DependencySpiderFeatureReceiver.DEPENDENCYSPIDERSPIDERENABLEDKEY))
                    {
                        cbSpiderEnabled.Checked = false;
                    }
                    else
                    {
                        bool enabledstate = false;
                        if (bool.TryParse(web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERSPIDERENABLEDKEY], out enabledstate))
                        {
                            cbSpiderEnabled.Checked = enabledstate;
                        }
                        else
                        {
                            cbSpiderEnabled.Checked = false;
                        }
                    }
                }
                #endregion

                #region ExcludeCrawlRules
                if (tbExcludeCrawlRules != null)
                {
                    if (!web.Properties.ContainsKey(DependencySpiderFeatureReceiver.DEPENDENCYSPIDEREXCLUDERULESKEY))
                    {
                        tbExcludeCrawlRules.Text = null;
                    }
                    else
                    {
                        tbExcludeCrawlRules.Text = web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDEREXCLUDERULESKEY];
                    }
                }
                #endregion

                #region IncludeCrawlRules
                if (tbIncludeCrawlRules != null)
                {
                    if (!web.Properties.ContainsKey(DependencySpiderFeatureReceiver.DEPENDENCYSPIDERINCLUDERULESKEY))
                    {
                        tbIncludeCrawlRules.Text = null;
                    }
                    else
                    {
                        tbIncludeCrawlRules.Text = web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERINCLUDERULESKEY];
                    }
                }
                #endregion
            }

        }

        protected override void CreateChildControls()
        {
            if (!IsPostBack)
            {
                SyncControls();
            }

            base.CreateChildControls();
        }

        private void CheckCrawlRules(string rules)
        {
            string[] _rules = rules.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
            
            foreach (string rule in _rules)
            {
                try
                {

                    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(rules);
                }
                catch (Exception ex)
                {
                    lblStatus.Text = string.Format("Failed parsing CrawlRule: {0}.\r\nError: {1}", rule, ex.Message);
                    break;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblStatus.Text = "";

            using (SPWeb web = Site.OpenWeb(Site.RootWeb.ID))
            {

                if (cbEnabled != null)
                {
                    web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERENABLEDKEY] = cbEnabled.Checked.ToString();
                }

                if (selReportingList != null)
                {
                    try
                    {
                        using (SPSite site = new SPSite(selReportingList.AssetUrl))
                        {
                            using (SPWeb web2 = site.OpenWeb())
                            {
                            }
                        }
                    }
                    catch
                    {
                        string testurl = SPUrlUtility.CombineUrl(SPContext.Current.Site.Url, selReportingList.AssetUrl);
                        try
                        {
                            using (SPSite site = new SPSite(selReportingList.AssetUrl))
                            {
                                using (SPWeb web2 = site.OpenWeb())
                                {
                                    selReportingList.AssetUrl = testurl;
                                }
                            }
                        }
                        catch
                        {
                            lblStatus.Text = string.Format("The Url '{0}' does not seem valid", selReportingList.AssetUrl);
                        }
                        
                    }

                    web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERREPORTKEY]= selReportingList.AssetUrl;
                }

                if (cbStoreMap != null)
                {
                    web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERSTOREMAPKEY] = cbStoreMap.Checked.ToString();
                }

                if (cbSpiderEnabled != null)
                {
                    web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERSPIDERENABLEDKEY]= cbSpiderEnabled.Checked.ToString();
                }

                if (tbExcludeCrawlRules != null)
                {
                    CheckCrawlRules(tbExcludeCrawlRules.Text);
                    web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDEREXCLUDERULESKEY]= tbExcludeCrawlRules.Text;
                }

                if (tbIncludeCrawlRules != null)
                {
                    CheckCrawlRules(tbIncludeCrawlRules.Text);
                    web.Properties[DependencySpiderFeatureReceiver.DEPENDENCYSPIDERINCLUDERULESKEY]= tbIncludeCrawlRules.Text;
                }

                web.Properties.Update();
            }

            SyncControls();
        }
    }
}
